﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Xml;
using EPiServer;
using EPiServer.Configuration;
using TemplateFoundation.Core;

namespace TemplateFoundation.MetaWeblog
{
    /// <summary>
    /// Handler for providing a RSD file
    /// </summary>
    public class ReallySimpleDiscoveryHandler : IHttpHandler
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.
        /// </returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            var writer = new XmlTextWriter(context.Response.Output);
            writer.WriteStartElement("rsd");
            writer.WriteStartAttribute("version");
            writer.WriteValue("1.0");
            writer.WriteEndAttribute();
            writer.WriteStartElement("service");
            writer.WriteStartElement("engineName");
            writer.WriteString("MetaWeblog for EPiServer through EPiServer Template Foundation");
            writer.WriteEndElement();
            writer.WriteStartElement("engineLink");
            writer.WriteString(Configuration.Settings.Instance.NewsContainer.GetPage().GetFriendlyUrl(true));
            writer.WriteEndElement();
            writer.WriteStartElement("homePageLink");
            writer.WriteString("http://etf.codeplex.com");
            writer.WriteEndElement();
            writer.WriteStartElement("apis");
            writer.WriteStartElement("api");
            writer.WriteStartAttribute("name");
            writer.WriteValue("MetaWeblog");
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("blogID");
            writer.WriteValue(Configuration.Settings.Instance.NewsContainer.ID.ToString());
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("preferred");
            writer.WriteValue("true");
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("apiLink");
            writer.WriteValue(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/metaweblog");
            writer.WriteEndAttribute();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
        }

        #endregion
    }
}
